{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Day02 基础练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Python基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1 变量与数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 示例题目：创建两个变量 `a` 和 `b`，分别赋值为 5 和 10，然后打印它们的和。\n",
    "a = 5\n",
    "b = 10\n",
    "print(\"它们的和是:\", a + b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目：创建两个变量a和b，分别赋值为5和10，然后打印它们的和。\n",
    "a = 5\n",
    "b = 10\n",
    "# 你的代码 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目：创建一个变量name，将你的名字赋值给它，然后打印一条欢迎信息，比如\"Hello, 张三!\"。\n",
    "name = \"张三\"\n",
    "# 你的代码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 输入输出\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 示例题目：写一个程序，要求用户输入他们的名字，并打印 \"Hello, [名字]\"。\n",
    "name = input(\"请输入你的名字: \")\n",
    "print(\"Hello,\", name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 题目：写一个程序，要求用户输入他们的名字和年龄，并打印\"Hello, [名字]，你的年龄是[年龄]\"。\n",
    "name = input(\"请输入你的名字: \")\n",
    "# 你的代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目：编写一个程序，要求用户输入两个数字，然后输出它们的乘积。\n",
    "num1 = input(\"请输入第一个数字: \")\n",
    "# 你的代码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3 条件语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 示例题目：写一个程序，判断一个用户输入的数字是奇数还是偶数。\n",
    "num = int(input(\"请输入一个数字: \"))\n",
    "if num % 2 == 0:\n",
    "    print(\"这个数字是偶数。\")\n",
    "else:\n",
    "    print(\"这个数字是奇数。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-30T02:07:43.839042Z",
     "iopub.status.busy": "2024-12-30T02:07:43.838289Z",
     "iopub.status.idle": "2024-12-30T02:07:43.844327Z",
     "shell.execute_reply": "2024-12-30T02:07:43.843245Z",
     "shell.execute_reply.started": "2024-12-30T02:07:43.839004Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目：编写一个程序，判断用户输入的数字是否大于10，如果是则打印 \"大于10\"，否则打印 \"不大于10\"。\n",
    "# 你的代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-30T02:08:18.808740Z",
     "iopub.status.busy": "2024-12-30T02:08:18.807322Z",
     "iopub.status.idle": "2024-12-30T02:08:18.812734Z",
     "shell.execute_reply": "2024-12-30T02:08:18.811740Z",
     "shell.execute_reply.started": "2024-12-30T02:08:18.808698Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目：判断一个数字是否为正数，如果是正数打印 \"正数\"，否则打印 \"不是正数\"。\n",
    "# 你的代码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.4 循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 示例题目：使用 for 循环打印数字 1 到 5。\n",
    "for i in range(1, 6):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目：使用 for 循环打印数字 1 到 50。\n",
    "# 你的代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-30T02:10:53.607872Z",
     "iopub.status.busy": "2024-12-30T02:10:53.607204Z",
     "iopub.status.idle": "2024-12-30T02:10:53.612785Z",
     "shell.execute_reply": "2024-12-30T02:10:53.611421Z",
     "shell.execute_reply.started": "2024-12-30T02:10:53.607835Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目：编写一个程序，打印从1到50的所有偶数。\n",
    "# 你的代码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.5 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 示例代码：定义一个函数 add，接受两个参数，返回它们的和。\n",
    "def add(a, b):\n",
    "    return a + b\n",
    "\n",
    "print(add(3, 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目：定义一个函数 minus，接受两个参数，返回它们的差。\n",
    "# 你的代码\n",
    "\n",
    "# 测试结果\n",
    "print(minus(5,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目：定义一个函数 square，接受一个数字作为参数，返回它的平方。\n",
    "# 你的代码\n",
    "\n",
    "# 测试结果\n",
    "print(square(50))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.6 数据结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目： 数组元素求和\n",
    "# 要求： 编写代码，计算一个数组中所有元素的和。\n",
    "\n",
    "# 完成以下代码，计算数组的和\n",
    "nums = [10, 20, 30, 40]\n",
    "total = 0\n",
    "for num in nums:\n",
    "    # \n",
    "    # 你的代码：将 num 加到 total 中\n",
    "    # \n",
    "print(\"数组的总和是:\", total)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Linux命令"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 查看当前目录\n",
    "!pwd\n",
    "%pwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 列出当前目录下的所有文件和文件夹\n",
    "%ls -a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 创建一个名为 test_folder 的新文件夹\n",
    "%mkdir test_folder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 切换到 test_folder 文件夹，查看该目录下所有文件\n",
    "%cd test_folder\n",
    "%ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-30T02:29:16.356542Z",
     "iopub.status.busy": "2024-12-30T02:29:16.355761Z",
     "iopub.status.idle": "2024-12-30T02:29:16.640514Z",
     "shell.execute_reply": "2024-12-30T02:29:16.638968Z",
     "shell.execute_reply.started": "2024-12-30T02:29:16.356506Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 创建一个名为 example.txt 的文件，并向其中写入 \"Hello, Linux!\"。\n",
    "!echo \"Hello, Linux!\" > example.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-30T02:30:31.108658Z",
     "iopub.status.busy": "2024-12-30T02:30:31.107956Z",
     "iopub.status.idle": "2024-12-30T02:30:31.424102Z",
     "shell.execute_reply": "2024-12-30T02:30:31.422629Z",
     "shell.execute_reply.started": "2024-12-30T02:30:31.108621Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 重命名 example.txt 为 hello.txt\n",
    "%mv example.txt hello.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-30T02:31:06.160423Z",
     "iopub.status.busy": "2024-12-30T02:31:06.159698Z",
     "iopub.status.idle": "2024-12-30T02:31:06.458893Z",
     "shell.execute_reply": "2024-12-30T02:31:06.457718Z",
     "shell.execute_reply.started": "2024-12-30T02:31:06.160374Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 复制hello.txt到hello_copy.txt\n",
    "%cp hello.txt hello_copy.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-30T02:31:31.955758Z",
     "iopub.status.busy": "2024-12-30T02:31:31.954427Z",
     "iopub.status.idle": "2024-12-30T02:31:32.249998Z",
     "shell.execute_reply": "2024-12-30T02:31:32.248807Z",
     "shell.execute_reply.started": "2024-12-30T02:31:31.955674Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 删除hello_copy.txt\n",
    "%rm hello_copy.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 题目：创建一个名为 test_folder2 的新文件夹，在该文件夹中新建一个叫做python_test.py 的文件，并向其中写入print('hello world')。\n",
    "# 你的代码\n",
    "\n",
    "# 测试运行结果\n",
    "!python python_test.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-30T02:35:22.521208Z",
     "iopub.status.busy": "2024-12-30T02:35:22.520505Z",
     "iopub.status.idle": "2024-12-30T02:35:22.525507Z",
     "shell.execute_reply": "2024-12-30T02:35:22.524750Z",
     "shell.execute_reply.started": "2024-12-30T02:35:22.521172Z"
    }
   },
   "source": [
    "## 3.数据集预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "!pwd\n",
    "!unzip /home/aistudio/data/data160305/列车手势数据集.zip -d /home/aistudio/work/railway-datasets/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# 定义数据集的主目录路径\n",
    "dataset_path = '/home/aistudio/work/railway-datasets'\n",
    "\n",
    "# 遍历主目录下的所有文件夹\n",
    "for folder in os.listdir(dataset_path):\n",
    "    # 构建子文件夹的完整路径\n",
    "    folder_path = os.path.join(dataset_path, folder)\n",
    "    \n",
    "    # 判断当前路径是否是文件夹\n",
    "    if os.path.isdir(folder_path):\n",
    "        # 获取当前文件夹中所有文件的数量\n",
    "        file_count = len(os.listdir(folder_path))\n",
    "        \n",
    "        # 打印文件夹名称和其中的文件数量\n",
    "        print(f\"文件夹 {folder} 包含 {file_count} 个文件\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "childdir = 'fache'  # 定义子文件夹名称为 'fache'\n",
    "\n",
    "# 构建子文件夹的完整路径\n",
    "files = os.listdir(dataset_path + '/' + childdir)  \n",
    "\n",
    "# 打印文件数量\n",
    "print(f\"{childdir} 文件夹中的文件数量: {len(files)}\")  \n",
    "\n",
    "# 打印前10个文件名称\n",
    "print(f\"前10个文件: {files[:10]}\")  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-30T03:08:15.284046Z",
     "iopub.status.busy": "2024-12-30T03:08:15.283438Z",
     "iopub.status.idle": "2024-12-30T03:08:15.288603Z",
     "shell.execute_reply": "2024-12-30T03:08:15.287718Z",
     "shell.execute_reply.started": "2024-12-30T03:08:15.284009Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目：输出jinzhanxinhao文件夹中的文件数量、前10个文件名称\n",
    "# 题目：输出tingchexinhao文件夹中的文件数量、前10个文件名称\n",
    "# 题目：输出yibiaozhengchang文件夹中的文件数量、前10个文件名称\n",
    "# 题目：输出zhenxiantinche文件夹中的文件数量、前10个文件名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-30T03:10:29.109346Z",
     "iopub.status.busy": "2024-12-30T03:10:29.108763Z",
     "iopub.status.idle": "2024-12-30T03:10:32.482367Z",
     "shell.execute_reply": "2024-12-30T03:10:32.481224Z",
     "shell.execute_reply.started": "2024-12-30T03:10:29.109312Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "\n",
    "#  对图像统一尺寸\n",
    "output_path = '/home/aistudio/work/railway-datasets-resized'\n",
    "os.makedirs(output_path, exist_ok=True)\n",
    "\n",
    "for folder in os.listdir(dataset_path):\n",
    "    folder_path = os.path.join(dataset_path, folder)\n",
    "    save_folder = os.path.join(output_path, folder)\n",
    "    os.makedirs(save_folder, exist_ok=True)\n",
    "    \n",
    "    for file in os.listdir(folder_path):\n",
    "        img_path = os.path.join(folder_path, file)\n",
    "        img = Image.open(img_path)\n",
    "        img = img.resize((128, 128))\n",
    "        img.save(os.path.join(save_folder, file))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 题目：对【AI达人特训营】交通灯检测的数据集进行预处理，如解压、查看数据集结构、同一尺寸等。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
